<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            clearable
            @clear="getGoodsList"
            placeholder="请输入内容"
            v-model="queryInfo.query"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="gotoAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表开始 -->

      <el-table :data="goodsList" stripe border>
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="价格" width="95px">
        </el-table-column>
        <el-table-column prop="goods_weight" label="重量" width="70px">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="140px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dataFormat }}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editGoods(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGoodsById(scope.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 商品列表结束 -->
      <!-- 分页开始 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
      <!-- 分页结束 -->
    </el-card>
  </div>
</template>

<script>
export default {
  created() {
    this.getGoodsList()
  },
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
      },
      //保存总数据条数
      total: 0,
      //保存商品列表
      goodsList: [],
    }
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http('goods', {
        params: this.queryInfo,
      })

      if (res.meta.status !== 200) {
        this.$message.info('获取商品列表数据失败')
      }

      this.goodsList = res.data.goods
      this.total = res.data.total
      console.log(this.goodsList)
      console.log(this.total)
    },
    //分页-处理每页大小发生变化事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    //分页-处理当前页码发生变化事件
    handleCurrentChange(currentNum) {
      this.queryInfo.pagenum = currentNum
      this.getGoodsList()
    },
    //删除指定商品
    async removeGoodsById(goodsId) {
      const confirm = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)

      if (confirm !== 'confirm') {
        return this.$message.info('已取消删除')
      }

      const { data: res } = await this.$http.delete(`goods/${goodsId}`)

      if (res.meta.status !== 200) {
        this.$message.error('删除商品失败')
      }

      this.$message.success('删除商品成功')

      this.getGoodsList()
    },
    //使用编程式导航跳转
    gotoAddPage() {
      this.$router.push('/goods/add')
    },
    //修改商品 todo：与添加商品一致
    editGoods(row) {},
  },
}
</script>
<style scoped>
</style>
